
using System;
using System.Drawing;
using System.Windows.Forms;


class Program {
    static void Main(){
        // Application.Run(new SimpleButton());
        // Application.Run(new TwoButtons());
        // Application.Run(new ButtonStyles());
        // Application.Run(new TwoButtonsAnchor());
        // Application.Run(new TwoButtonsDock());
        // Application.Run(new CheckBoxWithLabel());
        Application.Run(new CustomCheckBox());
    }
}

class SimpleButton:Form 
{
    public SimpleButton(){
        Text = "Simple Button";

        Button btn = new Button();
        // 基础的btn属性
        btn.Parent = this;
        btn.Text = "Click Me";
        btn.Location = new Point(100,100);
        btn.Click += new EventHandler(btnOnClick);
    }

    void btnOnClick(object obj,EventArgs ea){
        var g = CreateGraphics();
        var ptText = Point.Empty;
        var str = "Button Clicked";

        g.DrawString(str,Font,new SolidBrush(ForeColor),ptText);
        System.Threading.Thread.Sleep(250);
        g.FillRectangle(new SolidBrush(BackColor),
            new RectangleF(ptText,g.MeasureString(str,Font))
        );
        g.Dispose();
    }
}

class TwoButtons: Form 
{
    readonly Button btnLarger,btnSmaller;
    readonly int cxBtn,cyBtn,dxBtn;

    public TwoButtons(){
        Text = "Two Buttons";
        ResizeRedraw = true;

        cxBtn = 5 * Font.Height;
        cyBtn = 2 * Font.Height;

        dxBtn = Font.Height;

        btnLarger = new Button();
        btnLarger.Parent = this;
        btnLarger.Text = "&Larger";
        btnLarger.Size = new Size(cxBtn,cyBtn);

        btnLarger.Click += new EventHandler(btnOnclick);

        btnSmaller = new Button();
        btnSmaller.Parent = this;
        btnSmaller.Text = "&Smaller";
        btnSmaller.Size = new Size(cxBtn,cyBtn);

        btnSmaller.Click += new EventHandler(btnOnclick);
        
    }

    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);

        btnLarger.Location = new Point(
            ClientSize.Width/2 - cxBtn - dxBtn/2,
            (ClientSize.Height - cyBtn) /2
        );

        btnSmaller.Location = new Point(
            ClientSize.Width/2 + dxBtn/2,
            (ClientSize.Height - cyBtn) /2
        );
    }

    void btnOnclick(object obj,EventArgs ea){
        var btn = (Button)obj;

        if(btn == btnLarger){
            Left -= (int)(0.05*Width);
            Top -= (int)(0.05*Height);
            Width += (int)(0.01*Width);
            Height += (int)(0.01*Height);

        }else{
            Left += (int)(Width/22f);
            Top += (int)(Height/22f);
            Width -= (int)(Width/11f);
            Height -= (int)(Height/11f);
        }
    }
}

class ButtonStyles:Form {
    public ButtonStyles(){
        Text = "Button Styles";
        int y = 0;

        foreach(FlatStyle  fs in Enum.GetValues(typeof(FlatStyle))){
            var btn = new Button();
            btn.Parent = this;
            btn.FlatStyle = fs;
            btn.Text = fs.ToString();
            btn.Location = new Point(50,y+=50);
        }
    }
}


class DrawButtons: Form 
{
    readonly Button btnLarger,btnSmaller;
    readonly int cxBtn,cyBtn,dxBtn;

    public DrawButtons(){
        Text = "Two Buttons";
        ResizeRedraw = true;

        cxBtn = 5 * Font.Height;
        cyBtn = 2 * Font.Height;

        dxBtn = Font.Height;

        btnLarger = new Button();
        btnLarger.Parent = this;
        btnLarger.Text = "&Larger";
        btnLarger.Size = new Size(cxBtn,cyBtn);

        btnLarger.Click += new EventHandler(btnOnclick);

        btnSmaller = new Button();
        btnSmaller.Parent = this;
        btnSmaller.Text = "&Smaller";
        btnSmaller.Size = new Size(cxBtn,cyBtn);

        btnSmaller.Click += new EventHandler(btnOnclick);
        
    }

    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);

        btnLarger.Location = new Point(
            ClientSize.Width/2 - cxBtn - dxBtn/2,
            (ClientSize.Height - cyBtn) /2
        );

        btnSmaller.Location = new Point(
            ClientSize.Width/2 + dxBtn/2,
            (ClientSize.Height - cyBtn) /2
        );
    }

    void btnOnclick(object obj,EventArgs ea){
        var btn = (Button)obj;

        if(btn == btnLarger){
            Left -= (int)(0.05*Width);
            Top -= (int)(0.05*Height);
            Width += (int)(0.01*Width);
            Height += (int)(0.01*Height);

        }else{
            Left += (int)(Width/22f);
            Top += (int)(Height/22f);
            Width -= (int)(Width/11f);
            Height -= (int)(Height/11f);
        }
    }
}

class TwoButtonsAnchor: Form 
{
    readonly Button btnLarger,btnSmaller;
    readonly int cxBtn,cyBtn,dxBtn;

    public TwoButtonsAnchor(){
        Text = "Two Buttons with Anchor";
        ResizeRedraw = true;

        cxBtn = 5 * Font.Height;
        cyBtn = 2 * Font.Height;

        dxBtn = Font.Height;

        btnLarger = new Button();
        btnLarger.Parent = this;
        btnLarger.Text = "&Larger";
        btnLarger.Size = new Size(cxBtn,cyBtn);
        btnLarger.Location = new Point(dxBtn,dxBtn);
        btnLarger.Click += new EventHandler(btnOnclick);

        btnSmaller = new Button();
        btnSmaller.Parent = this;
        btnSmaller.Text = "&Smaller";
        btnSmaller.Size = new Size(cxBtn,cyBtn);
        btnSmaller.Location = new Point(
            ClientSize.Width - cxBtn-dxBtn,
            ClientSize.Height - cyBtn -dxBtn
        );
        btnSmaller.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;

        btnSmaller.Click += new EventHandler(btnOnclick);
        
    }

    void btnOnclick(object obj,EventArgs ea){
        var btn = (Button)obj;

        if(btn == btnLarger){
            Left -= (int)(0.05*Width);
            Top -= (int)(0.05*Height);
            Width += (int)(0.01*Width);
            Height += (int)(0.01*Height);

        }else{
            Left += (int)(Width/22f);
            Top += (int)(Height/22f);
            Width -= (int)(Width/11f);
            Height -= (int)(Height/11f);
        }
    }
}


class TwoButtonsDock: Form 
{
    readonly Button btnLarger,btnSmaller;
    readonly int cxBtn,cyBtn,dxBtn;

    public TwoButtonsDock(){
        Text = "Two Buttons with Anchor";
        ResizeRedraw = true;

        cxBtn = 5 * Font.Height;
        cyBtn = 2 * Font.Height;

        dxBtn = Font.Height;

        btnLarger = new Button();
        btnLarger.Parent = this;
        btnLarger.Text = "&Larger";
        btnLarger.Size = new Size(cxBtn,cyBtn);
        btnLarger.Dock = DockStyle.Top;
        btnLarger.Click += new EventHandler(btnOnclick);

        btnSmaller = new Button();
        btnSmaller.Parent = this;
        btnSmaller.Text = "&Smaller";
        btnSmaller.Size = new Size(cxBtn,cyBtn);
        btnSmaller.Dock = DockStyle.Bottom;
        btnSmaller.Click += new EventHandler(btnOnclick);
        
    }

    void btnOnclick(object obj,EventArgs ea){
        var btn = (Button)obj;

        if(btn == btnLarger){
            Left -= (int)(0.05*Width);
            Top -= (int)(0.05*Height);
            Width += (int)(0.01*Width);
            Height += (int)(0.01*Height);

        }else{
            Left += (int)(Width/22f);
            Top += (int)(Height/22f);
            Width -= (int)(Width/11f);
            Height -= (int)(Height/11f);
        }
    }
}

class CheckBoxWithLabel : Form 
{
    Label label;

    public CheckBoxWithLabel(){
        Text = "CheckBox Demo With Label";

        int cyText = Font.Height;
        int cxText = cyText/2;

        string[] astrText = {"Bold","Italic","Underline","Strikeout"};

        label = new Label();
        label.Parent = this;
        label.Text = Text +": Sample Text";
        label.AutoSize = true;

        for (int i = 0; i < 4; i++)
        {
            CheckBox chkBox = new CheckBox();
            chkBox.Parent = this;
            chkBox.Text = astrText[i];
            chkBox.Location = new Point(
                2 * cxText,
                (4 + 3 * i) * (cyText/2)
            );
            chkBox.Size = new Size(12 * cxText,cyText);
            chkBox.CheckedChanged += new EventHandler(CheckBoxOnCheckedChanged);
        }
    }

    void CheckBoxOnCheckedChanged(object obj,EventArgs ea){
        FontStyle fs = 0;
        FontStyle[] afs = {FontStyle.Bold,FontStyle.Italic,FontStyle.Underline,FontStyle.Strikeout};

        for (int i = 0; i < 4; i++)
        {
            if(((CheckBox)Controls[i+1]).Checked){
                fs = afs[i];
            }
        }

        label.Font = new Font(label.Font,fs);
    }       
}

class FontStyleCheckBox:CheckBox{
    public FontStyle fontstyle;
}

class CustomCheckBox : Form 
{
    public CustomCheckBox(){
        Text = "CustomCheckBox";

        int cyText = Font.Height;
        int cxText = cyText/2;

      

        FontStyle[] afs = {FontStyle.Bold,FontStyle.Italic,FontStyle.Underline,FontStyle.Strikeout};
        

        Label label = new Label();
        label.Parent = this;
        label.Text = Text +": Sample Text";
        label.AutoSize = true;

        for (int i = 0; i < 4; i++)
        {
            FontStyleCheckBox chkBox = new FontStyleCheckBox();
            chkBox.Parent = this;
            chkBox.Text = afs[i].ToString();
            chkBox.fontstyle = afs[i];

            chkBox.Location = new Point(
                2 * cxText,
                (4 + 3 * i) * (cyText/2)
            );
            chkBox.Size = new Size(12 * cxText,cyText);
            chkBox.CheckedChanged += new EventHandler(CheckBoxOnCheckedChanged);
        }
    }

    void CheckBoxOnCheckedChanged(object obj,EventArgs ea){
        FontStyle fs = 0;
        Label label = null;

        for (int i = 0; i < Controls.Count; i++)
        {   
            Control ctrl = Controls[i];

            // 读取Label
            if(ctrl.GetType() == typeof(Label)){
                label = (Label) ctrl;
            }else if(ctrl.GetType() == typeof(FontStyleCheckBox)){
                if(((FontStyleCheckBox)ctrl).Checked){
                    fs = ((FontStyleCheckBox)ctrl).fontstyle;
                }
            }
        }

        label.Font = new Font(label.Font,fs);
    }       
}

class AutoScaleDemo : Form 
{
    public AutoScaleDemo()
    {
        Text = " AUto Scale Demo";
        Font = new Font("Arial",12);
        FormBorderStyle = FormBorderStyle.FixedSingle;

        int[] aiPointSize = {8,12,16,24,32};

        for (int i = 0; i < aiPointSize.Length; i++)
        {
            Button btn = new Button();
            btn.Parent = this;
            btn.Text = "Use" + aiPointSize[i] +"-point font";
            btn.Tag = aiPointSize[i];
            btn.Location = new Point(4,16+24*i);
            btn.Size = new Size(80,16);
            btn.Click += new EventHandler(ButtonOnClick);
        }

        ClientSize = new Size(88,16+24*aiPointSize.Length);
    }

    void ButtonOnClick(object obj,EventArgs ea){
        Button  btn = (Button) obj;

        
    }
}